import 'dart:core';
import 'dart:typed_data';
import 'package:crypto/crypto.dart';
import 'dart:convert';

extension RegExpString on String{
  /*是否是手机号*/
   bool isPhone() {
     var pattern =
     new RegExp(r"^((13[0-9])|(15[^4,\D])|(18[0,0-9])|(17[0,0-9]))\d{8}$");
     return pattern.hasMatch(this);
   }

   /*是否是email*/
   bool isEmail(){
     var pattern = new RegExp(r"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}");
     return pattern.hasMatch(this);
   }

   /*检查纯数字*/
   bool isNum(){
     var pattern = new RegExp(r"^[0-9]*$");
     return pattern.hasMatch(this);
   }

   /*检查数或字母*/
   bool isNumOrString(){
     var pattern = new RegExp(r"^[A-Za-z0-9]+$");
     return pattern.hasMatch(this);
   }

   /*检查数或字母*/
   bool checkNumOrString(int start, int end){
     var pattern = new RegExp(r"^[A-Za-z0-9]{"+ start.toString()+","+end.toString() + r"}$");
     return pattern.hasMatch(this);
   }
   /*
  * param str:字符串
  * param start:开始范围
  * param end:结束范围
  * start - end位字母和数字组合
  * */
   bool checkNumAndString(int start, int end){
     String regex = r"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{"+ start.toString()+","+end.toString() + r"}$";
     var pattern = new RegExp(regex);
     return pattern.hasMatch(this);
   }

   /*是否包含标点*/
    bool containPunctuation(){
     String regex = "^.*[·\\~\\！\\@\\#\\￥\\%\\……\\&\\*\\（\\）\\——\\-\\+\\=\\【\\】\\{\\}\\、\\|\\；\\‘\\’\\：\\“\\”\\《\\》\\？\\，\\。\\、\\`\\~\\!\\#"+r"$"+"\\%\\^\\&\\*\\(\\)\\_\\[\\]\\{\\}\\|\\;\\'\\'\\:\\""\\,\\.\\/\\<\\>\\?].*"+ r"$";
     var pattern = new RegExp(regex);
     return pattern.hasMatch(this);
   }

   /*过滤掉标点符号*/
   String disablePunctuation(){
     String regex = "[^·\\~\\！\\@\\#\\￥\\%\\……\\&\\*\\（\\）\\——\\-\\+\\=\\【\\】\\{\\}\\、\\|\\；\\‘\\’\\：\\“\\”\\《\\》\\？\\，\\。\\、\\`\\~\\!\\#"+r"$"+
         "\\%\\^\\&\\*\\(\\)\\_\\[\\]\\{\\}\\|\\;\\""\\'\\'\\:\\,\\.\\/\\<\\>\\?]";
     var pattern = new RegExp(regex);
     String modifiedString = this.replaceAll(pattern, "");
     if (modifiedString.length == 0) {
       modifiedString = "";
     }
     return modifiedString;
   }

   /*是否包含表情符号*/
   bool isContainsEmoji(){
     String regex = r"(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])";
     var pattern = new RegExp(regex);
     return this.contains(pattern);
   }

   /*过滤掉表情符号*/
   String disableEmoji(){
     String regex = r"(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])";
     var pattern = new RegExp(regex);
     String modifiedString = this.replaceAll(pattern, "");
     if (modifiedString.length == 0) {
       modifiedString = "";
     }
     return modifiedString;
   }

   /*是否是url地址*/
   bool isUrl(){
     String regex = "(([H,h]ttp[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#"+r"$"+
         "%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#"+r"$"+
         "%^&*+?:_/=<>]*)?)";
     var pattern = new RegExp(regex);
     return pattern.hasMatch(this);
   }
}

extension HashString on String{
  /*md5加密*/
  String md5String(){
    var bytes = utf8.encode(this);
    var digest = md5.convert(bytes);
    return digest.toString();
  }

  /*sha1加密*/
  String sha1String(){
    var bytes = utf8.encode(this);
    var digest = sha1.convert(bytes);
    return digest.toString();
  }

  /*sha224加密*/
  String sha224String(){
    var bytes = utf8.encode(this);
    var digest = sha224.convert(bytes);
    return digest.toString();
  }

  /*sha256加密*/
  String sha256String(){
    var bytes = utf8.encode(this);
    var digest = sha256.convert(bytes);
    return digest.toString();
  }

  /*sha384加密*/
  String sha384String(){
    var bytes = utf8.encode(this);
    var digest = sha384.convert(bytes);
    return digest.toString();
  }

  /*sha512加密*/
  String sha512String(){
    var bytes = utf8.encode(this);
    var digest = sha512.convert(bytes);
    return digest.toString();
  }

  /*hmac md5加密*/
  String hmacMD5StringWithKey(String key){
    var k = utf8.encode(key);
    var bytes = utf8.encode(this);
    var hmacMD5 = Hmac(md5, k);
    var digest = hmacMD5.convert(bytes);
    return digest.toString();
  }

  /*hmac sha1加密*/
  String hmacSHA1StringWithKey(String key){
    var k = utf8.encode(key);
    var bytes = utf8.encode(this);
    var hmacSha1 = Hmac(sha1, k);
    var digest = hmacSha1.convert(bytes);
    return digest.toString();
  }

  /*hmac sha224加密*/
  String hmacSHA224StringWithKey( String key){
    var k = utf8.encode(key);
    var bytes = utf8.encode(this);
    var hmacSha224 = Hmac(sha224, k);
    var digest = hmacSha224.convert(bytes);
    return digest.toString();
  }

  /*hmac sha256加密*/
  String hmacSHA256StringWithKey( String key){
    var k = utf8.encode(key);
    var bytes = utf8.encode(this);
    var hmacSha256 = Hmac(sha256, k);
    var digest = hmacSha256.convert(bytes);
    return digest.toString();
  }

  /*hmac sha384加密*/
  String hmacSHA384StringWithKey( String key){
    var k = utf8.encode(key);
    var bytes = utf8.encode(this);
    var hmacSha384 = Hmac(sha384, k);
    var digest = hmacSha384.convert(bytes);
    return digest.toString();
  }

  /*hmac sha512加密*/
  String hmacSHA512StringWithKey(String key){
    var k = utf8.encode(key);
    var bytes = utf8.encode(this);
    var hmacSha512 = Hmac(sha512, k);
    var digest = hmacSha512.convert(bytes);
    return digest.toString();
  }

  /*base64 加密*/
  String base64EncodedString(){
    var bytes = utf8.encode(this);
    return base64Encode(bytes);
  }

  /*base64 解密*/
  String base64DecodeString(){
    Uint8List data = base64Decode(this);
    return data.toString();
  }
}

